﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using DG.Tweening.Plugins.Core.PathCore;
using System;

namespace Assets.Scripts.EffectorObject
{
    /// <summary>
    /// 投掷物
    /// </summary>
    public class Missile : BaseEffectorObject
    {
        public MissileData data;
        private bool isInited = false;
        private float duration;
        private Vector3 targetPos;
        private Vector3 midPointPos;
        private LineRenderer lineRenderer;
        private List<Vector3> path;
        private List<GameObject> linePointSpriteList;
        private Action onCompleteEvent;
        public void Start()
        {

        }
        public void Update()
        {

        }

        public void Init(MissileData data,Action onCompleteEvent = null, float duration = 1.0f)
        {
            this.data = data;
            this.duration = duration;
            if (data.lineType == LineType.DashedLine)
            {
                linePointSpriteList = new List<GameObject>();
                for (int i = 0; i < data.dashedLinePointCount; i++)
                {
                    GameObject o = GameObject.Instantiate(data.linePointSprite, GameObject.Find("Tip").transform);
                    o.SetActive(false);
                    linePointSpriteList.Add(o);
                }
            }
            else
            {
                lineRenderer = gameObject.AddComponent<LineRenderer>();
                lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
                lineRenderer.colorGradient = data.lineColor;
                lineRenderer.startWidth = 0.1f;
                lineRenderer.endWidth = 0.1f;
                lineRenderer.numCapVertices = 90;
                lineRenderer.sortingLayerName = "Indicator";
                lineRenderer.enabled = false;
            }
            isInited = true;
        }
        public void SetTargetPos(Vector3 targetPos)
        {
            if (!isInited) return;
            this.targetPos = targetPos;
            midPointPos = (transform.position + targetPos) / 2 + Vector3.up * (targetPos - transform.position).magnitude * data.midPointT;
            GetPath();
        }
        public void GetPath()
        {
            if (path == null)
            {
                path = new List<Vector3>();
            }
            path.Clear();
            if (data.lineType == LineType.DashedLine)
            {
                for (int i = 0; i < data.dashedLinePointCount; i++)
                {
                    path.Add(Util.GetBezierPoint((float)i / (float)(data.dashedLinePointCount - 1), transform.position, midPointPos, targetPos));
                }
            }
            else
            {
                for (int i = 0; i < data.solidlinePointCount; i++)
                {
                    path.Add(Util.GetBezierPoint((float)i / (float)(data.solidlinePointCount - 1), transform.position, midPointPos, targetPos));
                }
            }
        }
        public void ShowIndicator()
        {
            if (!isInited) return;
            if (data.lineType == LineType.DashedLine)
            {
                for (int i = 0; i < path.Count; i++)
                {
                    linePointSpriteList[i].transform.position = path[i];
                    linePointSpriteList[i].SetActive(true);
                }
            }
            else
            {
                lineRenderer.positionCount = path.Count;
                lineRenderer.SetPositions(path.ToArray());
                lineRenderer.enabled = true;
            }
        }
        public void HideIndicator()
        {
            if (!isInited) return;
            if (data.lineType == LineType.DashedLine)
            {
                foreach (GameObject item in linePointSpriteList)
                {
                    Destroy(item);
                }
            }
            else
            {
                lineRenderer.enabled = false;
            }
        }
        public void Throw()
        {
            if (!isInited) return;
            if (path != null)
            {
                var tween = transform.DOPath(new Path(PathType.Linear, path.ToArray(), 1), duration).SetEase(Ease.Linear);
                tween.onComplete += () =>
                {
                    onCompleteEvent?.Invoke();
                    if(data.onCompletedFuncNameList != null)
                    {
                        foreach (var item in data.onCompletedFuncNameList)
                        {
                            SendMessage(item, SendMessageOptions.DontRequireReceiver);
                        }
                    }
                    Destroy(this);
                };
                if (data.isRotate)
                {
                    tween.onUpdate += () =>
                    {
                        transform.Rotate(new Vector3(0, 0, data.rotateValue));
                    };
                }
                if (data.isScale)
                {
                    if (data.isReverse)
                    {
                        transform.DOScale(data.endScale, duration).From();
                    }
                    else
                    {
                        transform.DOScale(data.endScale, duration);
                    }
                }
            }
        }
    }
}